import { asyncRoutes, constantRoutes } from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const componentsMap = {
  plantIndex: () => import('@/views/plantLib/plantData/list'),
  plantAdd: () => import('@/views/plantLib/plantData/add'),
  plantDetail: () => import('@/views/plantLib/plantData/detail'),
  plantEdit: () => import('@/views/plantLib/plantData/edit'),
  identify: () => import('@/views/plantLib/identify/list'),
  identifyDetail: () => import('@/views/plantLib/identify/detail'),
  skill: () => import('@/views/plantLib/skill/menu'),
  skillList: () => import('@/views/plantLib/skill/list'),
  skillDetail: () => import('@/views/plantLib/skill/detail'),
  skillManage: () => import('@/views/plantLib/skill/manage/list'),
  skillEdit: () => import('@/views/plantLib/skill/manage/edit'),
  questionList: () => import('@/views/question/list'),
  questionDetail: () => import('@/views/question/detail'),
  questionAnswer: () => import('@/views/question/answer'),
  messageList: () => import('@/views/message/list'),
  messageDetail: () => import('@/views/message/detail'),
  messageCreate: () => import('@/views/message/create'),
  messageReply: () => import('@/views/message/reply'),
  systemAccount: () => import('@/views/system/account'),
  systemRole: () => import('@/views/system/role'),
  systemInformation: () => import('@/views/system/information'),
}

/**
 *将后台的路由表进行格式化
 * @param {*} asyncRouterMap
 */
function convertRouter(asyncRouterMap) {
  const accessedRouters = []
  if (asyncRouterMap) {
    asyncRouterMap.forEach(item => {
      var parent = generateRouter(item, true)
      var children = []
      if (item.children) {
        item.children.forEach(child => {
          children.push(generateRouter(child, false))
        })
      }
      parent.children = children
      accessedRouters.push(parent)
    })
  }
  accessedRouters.push({ path: '*', redirect: '/404', hidden: true })
  return accessedRouters
}

function generateRouter(item, isParent) {
  var router = {
    path: item.path,
    name: item.name,
    meta: item.meta,
    hidden: item.hidden || false,
    redirect: item.redirect,
    component: isParent ? () => import('@/layout') : componentsMap[item.name]
  }
  if (router.redirect === '') {
    delete router.redirect
  }
  // console.log(router)
  return router
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  // asyncRoutes后期从后端获取
  generateRoutes({ commit }, menuList) {
    return new Promise(resolve => {
      const accessedRouters = convertRouter(menuList)
      console.log(accessedRouters, asyncRoutes)
      // console.log(asyncRoutes)
      let accessedRoutes
      // if (roles.includes('admin')) {
      //   accessedRoutes = asyncRoutes || []
      // } else {
      //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // }
      accessedRoutes = accessedRouters
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
